package cn.hxzy.assignperson.mapper;

import cn.hxzy.common.domain.*;
import cn.hxzy.common.domain.ClassMenu;
import cn.hxzy.common.domain.ClassTable;
import cn.hxzy.common.domain.ClassTeacher;
import cn.hxzy.common.domain.TeacherMajor;
import cn.hxzy.common.domain.vo.FuzeVo;
import cn.hxzy.common.domain.vo.InsertVo;
import cn.hxzy.common.domain.vo.TeacherVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

public interface TeacherMajorMapper {
    //    查询专业表，老师表，第三方表
    @Select("SELECT\n" +
            "\tdirection_name,sys_user.nick_name,sys_user.user_id\n" +
            "FROM\n" +
            "\tsys_direction,\n" +
            "\tsys_teacher_major,\n" +
            "\tsys_user\n" +
            "WHERE\n" +
            "\tsys_direction.direction_id = sys_teacher_major.major_id AND\n" +
            "\tsys_teacher_major.teacher_id = sys_user.user_id and user_status = 0 ")
    List<FuzeVo> selectAllGl();

    @Select("select * from sys_school")
    List<School> selectAllSchool();

//    添加sys_user表并且获取新增的id

    @Insert("insert into sys_user(user_name,nick_name,user_pass,user_card,school_id,user_email,user_phone," +
            "user_sex,user_avatar,user_type,user_status)" +
            " values(#{userName},#{nickName},#{userPass},#{userCard},#{schoolId},#{userEmail},#{userPhone}" +
            ",#{userSex},#{userAvatar},#{userType},#{userStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int insertUser(InsertVo insertVo);

    //    新增sys_teacher_major表
    @Insert("insert into sys_teacher_major(teacher_id,major_id) values(#{userId},#{majorId})")
    int insertTeacherMajor(Integer userId, Integer majorId);

    //根据id查询信息
    @Select("select * from sys_user where user_id = #{id}")
    User selectById(Integer id);

    //编辑功能
    @Select("SELECT\n" +
            "\t*, \n" +
            "\tsys_teacher_major.major_id\n" +
            "FROM\n" +
            "\tsys_user,\n" +
            "\tsys_teacher_major,\n" +
            "\tsys_direction\n" +
            "WHERE\n" +
            "\tsys_user.user_id = #{id} AND\n" +
            "\tsys_teacher_major.major_id = sys_direction.direction_id AND\n" +
            "\tsys_teacher_major.teacher_id = #{id}\n")
    InsertVo selectByIdall(Integer id);

    //修改信息
    @Update("update sys_user set" +
            " user_name = #{userName},nick_name = #{nickName}," +
            "user_pass = #{userPass},user_card = #{userCard}," +
            "school_id = #{schoolId},user_email = #{userEmail}," +
            "user_phone = #{userPhone},user_sex = #{userSex}," +
            "user_avatar = #{userAvatar},user_type = #{userType}," +
            "user_status = #{userStatus} where user_id = #{userId}")
    int updateUser(InsertVo insertVo);

    //    修改专业
    @Update("update sys_teacher_major set major_id = #{majorId} where teacher_id = #{userId}")
    int updateMajor(InsertVo insertVo);

    //    删除
    @Delete("delete from sys_user where user_id = #{id}")
    int deleteUser(Integer id);

    @Delete("delete from sys_teacher_major where teacher_id = #{id}")
    int deleteTeacherMajor(Integer id);

    //    查询所有方向
    @Select("select * from sys_direction")
    List<Direction> selectAllDirection();

    //    查询所有老师的名字
    @Select("select user_id, sys_user.nick_name from sys_user where user_type=1")
    List<User> selectAllTeacher();

    // 查询教研组组长
    @Select("select *from sys_teacher_major where major_id=#{tid} and teachering_research_id=1 ")
    TeacherMajor selectteacherGl(Integer tid);

    //   获取老师信息
    @Select("select nick_name from sys_user where user_id=#{tid}")
    User selectTeacherNames(Integer tid);

    //    添加专业
    @Insert("insert into sys_direction(direction_name) VALUES (#{directionName})")
    int addDirection(String directionName);

    //    修改专业名称
    @Update("update sys_direction set direction_name=#{directionName} where direction_id=#{directionId} ")
    int updateDirection(Direction direction);

    //
    @Update("update sys_teacher_major set teachering_research_id=1 where teacher_id=#{userId}")
    int updateTeacheringResearchId(Integer userId);

    //清空指定专业的老师中间表的教研组
    @Update("update sys_teacher_major " +
            "set teachering_research_id = 0 " +
            "where  major_id = #{mid}")
    void updateAll(Integer mid);

    //根据专业id查询老师id
    @Select("select teacher_id from sys_teacher_major where major_id = #{mid}")
    List<Integer> selectTeachersId(Integer mid);
//管理员向上
//===============================================================================================================
//教研组组长向下


    // 根据专业id查询所有班级
    @Select("SELECT * \n" +
            "FROM `sys_class` \n" +
            "where marjor_id = #{mid}")
    List<ClassTable> selectAllClassByMarjorId(Integer id);

    //根据班级id查询老师id
    @Select("select teacher_id from sys_class_teacher where class_id=#{tid}")
    List<Integer> selectTeacherNameByClassId(Integer tid);

    //根据老师id去用户表查询老师名字
    @Select("select nick_name from sys_user where user_id=#{tid}")
    String selectTeacherNameByUserId(Integer tid);

    //根据专业id查询老师id
    @Select("select teacher_id from sys_teacher_major where major_id = #{mid}")
    List<Integer> selectTeacherIdByMajorId(Integer mid);

    //根据老师id去用户表查询老师名字
    @Select("select user_id,nick_name from sys_user where user_id=#{tid}")
    TeacherVo selectTeacherMessage(Integer tid);

    //分配老师的方法
    //新增班级老师
    @Insert("<script>" +
            "insert into sys_class_teacher(class_id, teacher_id) values " +
            "<foreach collection='classTeachers' item='item' separator=','>" +
            "(#{item.classId}, #{item.teacherId})" +
            "</foreach>" +
            "</script>")
    int addTeachersInClass(@Param("classTeachers") List<ClassTeacher> classTeachers);

    //清空班级分配的老师
    @Delete("delete from sys_class_teacher where class_id = #{classId}")
    int deleteTeachersInClass(Integer classId);


    //创建班级
    @Insert("insert into sys_class (class_name,create_time,school_id,marjor_id) " +
            "values (#{className},#{createTime},#{schoolId},#{marjorId})")
    Integer addOneClass(ClassTable classTable);

    //修改班级
    @Update("update sys_class set class_name = #{className} where class_id = #{classId} \n ")
    Integer updateOneClass(ClassTable classTable);

    @Select("select direction_name from sys_direction where direction_id=#{id}")
    Direction selectDirectionById(Integer id);



}
